#define DEBUG
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn=100000;

class Ele {
public:
  int v, id;

  static bool cmp(Ele a, Ele b) {
    return a.v<b.v || (a.v==b.v && a.id<b.id);
  }
};

int lowbit(int k) {
  return k&(-k);
}

void add(int *a, int t, int tv, int n) {
  for (; t<=n; t+=lowbit(t)) {
    a[t]+=tv;
  }
}

int get(int *a, int t) {
  int ret=0;
  for (; t>0; t-=lowbit(t)) {
    ret += a[t];
  }
  return ret;
}

int main() {
  freopen("time.in", "r", stdin);
  freopen("time.out", "w", stdout);

  static Ele eles[maxn+1];
  static int a[maxn+1];
  int n;
  scanf("%d", &n);
  for (int i=1; i<=n; i++) {
    scanf("%d", a+i);
    eles[i].v = a[i], eles[i].id = i;
  }
  sort(eles+1, eles+n+1, Ele::cmp);

  static int sum[maxn+1];
  int ans=0;
  for (int i=1; i<=n; i++) {
    add(sum, i, 1, n);
  }
  for (int i=1; i<=n; i++) {
    int l=i, r=i;
    for (; r<=n && eles[r].v==eles[i].v; r++);
    r--;
    i = r;
    for (; l<=r; ) {
      int disa=get(sum, eles[l].id-1), disb=get(sum, n)-get(sum, eles[r].id);
      if (disa<disb) {
	ans += disa;
	add(sum, eles[l].id, -1, n);
	l++;
      } else {
	ans += disb;
	add(sum, eles[r].id, -1, n);
	r--;
      }
    }
  }

  printf("%d", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
